﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;

namespace FlightSearchServer
{
    public class FlightSearchHost
    {
        private const int NUMBER_OF_ARGUMENTS = 2;

        private const int CLIENT_PORT_INDEX = 0;

        private const int SERVER_PORT_INDEX = 1;

        static public void Main(string[] args)
        {
            if (args.Length != NUMBER_OF_ARGUMENTS)
            {
                Console.WriteLine( "Incorrect input format");
                Console.WriteLine( "Expected format: <client port> <server port>");
                return;
            }

            try
            {
                Convert.ToInt32(args[CLIENT_PORT_INDEX]);
            }
            catch ( InvalidCastException e)
            {
                Console.WriteLine( "Invalid number format of client port.");
                return;
            }

            try
            {
                Convert.ToInt32(args[SERVER_PORT_INDEX]);
            }
            catch (InvalidCastException e)
            {
                Console.WriteLine("Invalid number format of server port.");
                return;
            }

            using (ServiceHost host = new ServiceHost( typeof(FlightSearchService), new Uri( String.Format( "http://localhost:{0}/FlighServices", args[SERVER_PORT_INDEX])))) {
                try
                {
                    host.Open();
                    Console.ReadLine();
                } finally {
                    host.Close( );
                }
            }
        }
    }
}